package com.qingcheng.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.user.Address;
import com.qingcheng.pojo.user.User;
import com.qingcheng.service.user.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UsersController {

    @Reference
    private UserService userService;


    @GetMapping("/updateUser")
    public Result updateUser(String nickName,String password,String checkPassword){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        userService.updateUser(nickName,password,checkPassword,username);
        return new Result();
    }

    @PostMapping("/updateUserDetail")
    public Result updateUserDetail(@RequestBody User user){
        String  username = SecurityContextHolder.getContext().getAuthentication().getName();
        user.setUsername(username);
        userService.updateUserDetail(user);
        return new Result();
    }

    @GetMapping("/findUsername")
    public String findUsername(){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        String loginName = userService.findUsername(username);
        return loginName;
    }

    @GetMapping("/addressList")
    public List<Address> addressList(){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        return userService.addressList(username);
    }

    @GetMapping("/changeAddress")
    public Result changeAddress(String address) throws UnsupportedEncodingException {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        address = new String(address.getBytes("iso-8859-1"),"utf-8");
        userService.changeAddress(username,address);
        return new Result();
    }

    @GetMapping("/deleteAddress")
    public Result deleteAddress(String id){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        userService.deleteAddress(username,id);
        return new Result();
    }

    @PostMapping("/addAddress")
    public Result addAddress(@RequestBody Address address){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        userService.addAddress(username,address);
        return new Result();
    }

    @PostMapping("/updateAddress")
    public Result updateAddress(@RequestBody Address address){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        userService.updateAddress(username,address);
        return new Result();
    }

    @GetMapping("/loginName")
    public String loginName(){
        return SecurityContextHolder.getContext().getAuthentication().getName();
    }

    @GetMapping("/sendSms")
    public Result sendSms(String phone){
        userService.sendSms(phone);
        return new Result();
    }
    @GetMapping("/checkCode")
    public Result checkCode(String phone,String smsCode){
        userService.checkCode(phone,smsCode);
        return new Result();
    }

    @GetMapping("/setPhone")
    public Result setPhone(String newPhone){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        userService.setPhone(username,newPhone);
        return new Result();
    }

}
